package rdd.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Spark02_RDD_mapPartitionsWithIndex {
  def main(args: Array[String]): Unit = {
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
    val sc = new SparkContext(sparkConf)
    val rdd: RDD[Int] = sc.makeRDD(List(1, 2, 3, 4),3)
    val mapRDD = rdd.mapPartitionsWithIndex(
      (index, iter) => {
        //        if (index == 1) iter
        //        else Nil.iterator
        iter.map(
          num => {
            (index, num)
          }
        )

      }
    )
    mapRDD.collect().foreach(println)
    sc.stop()
  }
}
